﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Coolite.Ext.Web;
using Coolite.Utilities;
using System.Data;
using System.Configuration;

namespace Sagacity.NC.Web
{
    public partial class Index : PageBase
    {
        Sagacity.Build.User mUser = new Sagacity.Build.User();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                lblUserInfo.Text = "登陆用户：" + CurrentUser.UserName + "|";
                lblUserInfo.Text += "登陆角色：" + CurrentUser.LoginRole.Caption;
            }
        }

        protected void LoginOutSys(object sender, EventArgs e)
        {
            CurrentUser.SignOut();
            Response.Redirect("Login.aspx");
        }

        protected void btnLogin_Click(object sender, AjaxEventArgs e)
        {
            this.Window1.Hide();
            string oldpwd= this.txtoldpwd.Value.ToString();
            string newpwd=this.txtPassword.Value.ToString();
            string respwd= this.txtpwd.Value.ToString();
            try{
                if (newpwd == respwd)
                {
                    UpdatePassWord();
                }
                else
                    Ext.Msg.Alert("提示", "两次输入的新密码不正确！").Show();
            }catch(Exception ex){
                Ext.Msg.Alert("提示", "异常错误！"+ex.Message).Show();
            }

        }

        /// <summary>
        /// 修改用户的密码
        /// </summary>
        /// <returns></returns>
        public bool UpdatePassWord()
        {
            bool result = false;
            //string name = this.txtName.Text.Trim();
            //string before = this.txtPassWrod.Text.Trim();
            //string one = this.txtNewPwd.Text.Trim();
            //string pwd = this.txtRetype.Text.Trim();
            string oldpwd = this.txtoldpwd.Value.ToString();
            string newpwd = this.txtPassword.Value.ToString();
            string respwd = this.txtpwd.Value.ToString();

            Sagacity.Entity.Build.UserInfo user = (new Business.SysMange.SysUsers()).getUsers(CurrentUser.UserName);
            Sagacity.Entity.Build.UserInfo uinfo = mUser.UserLogin(CurrentUser.UserName, Sagacity.Common.AES.Encode(oldpwd, Config.BaseConfig.Instance.AES_EncryptKey));
           Business.SysMange.SysUsers sys = new Business.SysMange.SysUsers();
            if (uinfo != null)
            {
                string despwd = Sagacity.Common.AES.Encode(respwd, Config.BaseConfig.Instance.AES_EncryptKey);
                user.Password = despwd;
                Sagacity.Entity.Build.UserInfo userInfo = sys.UpdateUserPassWord(user, user.Id);
                if (userInfo != null)
                {
                    Ext.Msg.Alert("提示", "密码修改成功！").Show();
                    result = true;
                    return result;
                }
            }
            Ext.Msg.Alert("提示", "原密码错误！").Show();
            return result;
        }

        //验证数据库中是否存在此用户
        [AjaxMethod]
        public bool VildateUserName(string userName)
        {
            bool result = false;
            Business.SysMange.SysUsers shb = new Business.SysMange.SysUsers();
            result = shb.GetUsersByName(userName);
            return result;
        }
    }
}
